package com.example.a828502.sqliteimporttest;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Handler;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    public DBManager dbHelper;
    private List<SelectData> datas =new ArrayList<SelectData>();
    private ListView listView;
    private Button mAdd,mUpdate,mDelete,mQuery,mQueryCursor;
    private TextView textShow;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mAdd = (Button) findViewById(R.id.btnAdd);
        mUpdate = (Button) findViewById(R.id.btnUpdate);
        mDelete = (Button) findViewById(R.id.btnDelete);
        mQuery = (Button) findViewById(R.id.btnQuery);
        mQueryCursor = (Button) findViewById(R.id.btnQueryCursor);
        textShow = (TextView) findViewById(R.id.textShow);
        listView = (ListView) findViewById(R.id.listView);

        //添加点击事件
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                    long arg3) {
                //获得选中项的HashMap对象，arg2是选中的位置
                HashMap<String,String> map=(HashMap<String,String>)listView.getItemAtPosition(arg2);
                String title=map.get("select_name");
                //获取数据对象
               SelectData data = datas.get(arg2);
               textShow.setText("商家名称是"+data.select_name + "商家编码是"+data.select_id);

            }

        });
        mAdd.setOnClickListener(this);
        mUpdate.setOnClickListener(this);
        mDelete.setOnClickListener(this);
        mQuery.setOnClickListener(this);
        mQueryCursor.setOnClickListener(this);
        //首次执行导入.db文件
        dbHelper = new DBManager(this);
        dbHelper.openData();
//        dbHelper.closeDatabase();

    }

    @Override
    public void onClick(View view){
        switch (view.getId()){
            case R.id.btnAdd:
//                add();
                addData();
                break;
            case R.id.btnUpdate:
//                update();
                updateData();
                break;
            case R.id.btnDelete:
//                delete();
                deleteData();
                break;
            case R.id.btnQuery:
//                query();
                //存储数组对象，选中
                querySelectData();
                break;
            case R.id.btnQueryCursor:
//                queryCursor();
                break;
            default:
                break;
        }
    }

    @Override
    protected void onDestroy()
    {
        super.onDestroy();
        dbHelper.closeDatabase();// 释放数据库资源
    }

    public void add()
    {
        ArrayList<CityBean> citys = new ArrayList<CityBean>();

        CityBean city1 = new CityBean("Ella","lively girl");
        CityBean city2 = new CityBean("Jenny", "beautiful girl");
        CityBean city3 = new CityBean("Jessica","sexy girl");

        citys.add(city1);
        citys.add(city2);
        citys.add(city3);

        dbHelper.add(citys);

    }

    public void addData()
    {
        ArrayList<SelectData> datas = new ArrayList<SelectData>();

        SelectData data1 = new SelectData("0001","粉粉");
        SelectData data2 = new SelectData("0002","粉粉2");
        datas.add(data1);
        datas.add(data2);
        dbHelper.addData(datas);
        querySelectData();

    }


    public void update()
    {
        // 修改城市名称
        CityBean city = new CityBean();
        city.dist_cn_name = "天津";
        city.dist_code = "北京的旁边";
        dbHelper.updateCity(city);
    }

    public void updateData()
    {
        // 修改城市名称
        SelectData data = new SelectData();
        data.select_name = "粉粉";
        data.select_id = "00001111";
        dbHelper.updateData(data);
        querySelectData();
    }


    public void delete()
    {
        // 删除
        CityBean city = new CityBean();

        city.dist_cn_name = "北京";
        dbHelper.deleteAll(city);
    }

    public void deleteData()
    {
        // 删除
        SelectData data = new SelectData();

        data.select_name = "虾绅士";
        dbHelper.deleteAllData(data);
        querySelectData();
    }

    public void query()
    {
        List<CityBean> citys = dbHelper.query();
        ArrayList<Map<String, String>> list = new ArrayList<Map<String, String>>();
        for (CityBean city : citys)
        {
            HashMap<String, String> map = new HashMap<String, String>();
            map.put("dist_cn_name", city.dist_cn_name);
            map.put("dist_code", city.dist_code);
            list.add(map);
        }
        SimpleAdapter adapter = new SimpleAdapter(this, list,
                android.R.layout.simple_list_item_2, new String[] { "dist_cn_name",
                "dist_code" }, new int[] { android.R.id.text1,
                android.R.id.text2 });
        listView.setAdapter(adapter);
    }

    /**
     * 查询数据
     */
    public void querySelectData()
    {
     datas = dbHelper.querySelectData();
        ArrayList<Map<String, String>> list = new ArrayList<Map<String, String>>();
        for (SelectData data : datas)
        {
            HashMap<String, String> map = new HashMap<String, String>();
            map.put("select_id", data.select_id);
            map.put("select_name", data.select_name);
            list.add(map);
        }
        SimpleAdapter adapter = new SimpleAdapter(this, list,
                android.R.layout.simple_list_item_2, new String[] { "select_name",""
                 }, new int[] { android.R.id.text1,
                android.R.id.text2 });
        listView.setAdapter(adapter);
    }





    /**
     * 查询河南省  省市
     */
    public void queryCursor()
    {
        new android.os.Handler().postDelayed(new Runnable(){
            @Override
            public void run(){
                Toast.makeText(MainActivity.this, "测试延时功能", Toast.LENGTH_SHORT).show();
            }
        },5000);
        List<CityBean> citys = dbHelper.queryCursor();
        ArrayList<Map<String, String>> list = new ArrayList<Map<String, String>>();
        for (CityBean city : citys)
        {
            HashMap<String, String> map = new HashMap<String, String>();
            map.put("dist_cn_name", city.dist_cn_name);
            map.put("dist_code", city.dist_code);
            list.add(map);
        }
        SimpleAdapter adapter = new SimpleAdapter(this, list,
                android.R.layout.simple_list_item_2, new String[] { "dist_cn_name",
                "dist_code" }, new int[] { android.R.id.text1,
                android.R.id.text2 });
        listView.setAdapter(adapter);
    }

}

